package com.kefu.chat.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kefu.chat.vo.ThirdPartDemoVo;
import com.kefu.chat.vo.ThirdPartNodeVo;
import com.kefu.chat.vo.ThirdPartNodeVo.NodeItem;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;

import icu.mhb.mybatisplus.plugln.tookit.Lists;
import io.swagger.annotations.Api;

@Api(tags = "open")
@RestController
@RequestMapping("/open/robot")
public class ThirdPartDemoController {

	@PostMapping("nextNode")
	public Result<ThirdPartNodeVo> nextNode(@RequestHeader(name = "token",required = false) String token
										   ,@RequestBody ThirdPartDemoVo demoVo){
		if(!"123456".equals(token)) {
			return ResultGenerator.genFailResult("无效的token");
		}
		
		ThirdPartNodeVo node=null;
		if(StringUtils.isAllBlank(demoVo.getBusiness(),demoVo.getAddress(),demoVo.getDate())) {
			node=queryBusiness(demoVo);
		}else if(StringUtils.isAllBlank(demoVo.getAddress(),demoVo.getDate())) {
			node=queryAddress(demoVo);
		}else if(StringUtils.isAllBlank(demoVo.getDate())) {
			node=queryDate(demoVo);
		}else {
			node=build(demoVo);
		}
		
		return ResultGenerator.genSuccessResult(node);
	}
	
	ThirdPartNodeVo queryBusiness(ThirdPartDemoVo demoVo) {
		return ThirdPartNodeVo
					.builder()
					.nodeId(demoVo.getNodeId())
					.nodeType(1)
					.nodeName("业务办理")
					.nodeContent("请选择你要办理的业务")
					.bindName("business")
					.itemList(Lists.newArrayList(
							NodeItem
								.builder()
								.bindValue("测试1")
								.itemId("test1")
								.nodeId(demoVo.getNodeId())
								.nextNodeId(demoVo.getNodeId())
								.itemType(1)
								.itemContent("测试1")
							.build()
							))
				.build();
	}
	ThirdPartNodeVo queryAddress(ThirdPartDemoVo demoVo) {
		return ThirdPartNodeVo
				.builder()
				.nodeId(demoVo.getNodeId())
				.nodeType(1)
				.nodeName("业务办理")
				.nodeContent("请选择办理业务的地点")
				.bindName("address")
				.itemList(Lists.newArrayList(
						NodeItem
						.builder()
						.bindValue("上海")
						.itemId("上海")
						.nodeId(demoVo.getNodeId())
						.nextNodeId(demoVo.getNodeId())
						.itemType(1)
						.itemContent("上海")
						.build(),
						NodeItem
						.builder()
						.bindValue("北京")
						.itemId("北京")
						.nodeId(demoVo.getNodeId())
						.nextNodeId(demoVo.getNodeId())
						.itemType(1)
						.itemContent("北京")
						.build()
						))
				.build();
	}
	ThirdPartNodeVo queryDate(ThirdPartDemoVo demoVo) {
		return ThirdPartNodeVo
				.builder()
				.nodeId(demoVo.getNodeId())
				.nodeType(2)
				.nodeName("业务办理")
				.nodeContent("请选择办理时间")
				.bindName("date")
				.build();
	}
	
	ThirdPartNodeVo build(ThirdPartDemoVo demoVo) {
		return ThirdPartNodeVo
				.builder()
				.nodeId(demoVo.getNodeId())
				.nodeType(3)
				.nodeName("业务办理")
				.nodeContent(String.format("http://www.baidu.com?address=%s&business=%s&date=%s",demoVo.getAddress(),demoVo.getBusiness(),demoVo.getDate()))
				.build();
	}
	
}
